import java.util.*;

public class project {
    public int unhappyFriends(int n, int[][] preferences, int[][] pairs) {
        int[][] arr=new int[n][n];
        for(int i=0;i<n;i++){
            for(int j=0;j<n-1;j++){
                arr[i][preferences[i][j]]=j;
            }
        }
        int[] match=new int[n];
        for(int[] pair:pairs){
            match[pair[0]]=pair[1];
            match[pair[1]]=pair[0];
        }
        int ret=0;
        for(int x=0;x<n;x++){
            int y=match[x];
            int index=arr[x][y];
            for(int i=0;i<index;i++){
                int u=preferences[x][i];
                int v=match[u];
                if(arr[u][x]<arr[u][v]){
                    ret++;
                    break;
                }
            }
        }
        return ret;
    }


















    public long[] sumOfThree(long num) {
        if(num%3!=0){
            return new long[0];
        }
        return new long[]{num/3-1,num/3,num/3+1};
    }













    public int maxIceCream(int[] costs, int coins) {
        Arrays.sort(costs);
        int ret=0;
        for(int i=0;i<costs.length;i++){
            if(costs[i]<=coins){
                coins-=costs[i];
                ret++;
            }else{
                break;
            }
        }
        return ret;
    }




















    public List<List<Integer>> queensAttacktheKing(int[][] queens, int[] king) {
        List<List<Integer>> list=new ArrayList<>();
        boolean[][] booleans=new boolean[8][8];
        for(int[] queen:queens){
            booleans[queen[0]][queen[1]]=true;
        }
        int[][] d=new int[][]{{0,-1},{0,1},{-1,0},{1,0},{-1,-1},{1,1},{-1,1},{1,-1}};
        for(int i=0;i<d.length;i++){
            for(int x=king[0],y=king[1];x>=0&&x<8&&y>=0&&y<8;x+=d[i][0],y+=d[i][1]){
                if(booleans[x][y]){
                    list.add(Arrays.asList(x,y));
                    break;
                }
            }
        }
        return list;
    }
























    public int[] platesBetweenCandles(String s, int[][] queries) {
        int len=s.length();
        int[] prevs=new int[len];
        int sum=0;
        for(int i=0;i<len;i++){
            if(s.charAt(i)=='*'){
                sum++;
            }
            prevs[i]=sum;
        }
        int[] left=new int[len];
        int value=-1;
        for(int i=0;i<len;i++){
            if(s.charAt(i)=='|'){
                value=i;
            }
            left[i]=value;
        }
        int[] right=new int[len];
        value=-1;
        for(int i=len-1;i>=0;i--){
            if(s.charAt(i)=='|'){
                value=i;
            }
            right[i]=value;
        }
        int[] ret=new int[queries.length];
        for(int i=0;i<queries.length;i++){
            int[] querie=queries[i];
            int x=right[querie[0]];
            int y=left[querie[1]];
            ret[i]=x==-1||y==-1||x>=y?0:prevs[y]-prevs[x];
        }
        return ret;
    }
}
